package com.choichumgming.rentcat.web.controller;


import com.choichumgming.rentcat.common.api.CommonResult;
import com.choichumgming.rentcat.model.PmsComment;
import com.choichumgming.rentcat.service.PmsCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName PmsCommentController
 * @Description : 管理商品評論功能
 * @Author : choichumgming
 * @Date : 1/3/2020 11:57
 */
@Controller
@Api(tags = "PmsCommentController", description = "商品评论管理")
@RequestMapping("/comment")
public class PmsCommentController {
    @Autowired
    PmsCommentService pmsCommentService;
    @ApiOperation("添加商品评论")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult add(@RequestBody PmsComment comment) {
        int count = pmsCommentService.add(comment);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除商品评论")
    @RequestMapping(value = "/delete/{commentId}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long commentId) {
        int count = pmsCommentService.delete(commentId);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("获得商品所有评论")
    @RequestMapping(value = "/list/{productId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsComment>> getList(@PathVariable("productId") Long productId) {
        List<PmsComment> list = pmsCommentService.getProdcutComments(productId);
        if (list.size() > 0) {
            return CommonResult.success(list);
        }
        return CommonResult.failed();
    }


    @ApiOperation("获取评论详情")
    @RequestMapping(value = "/item/{commentId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsComment> item(@PathVariable("commentId") Long commentId) {
        PmsComment comment = pmsCommentService.getItem(commentId);
        return CommonResult.success(comment);
    }


    @ApiOperation("显示指定用户所有评论")
    @RequestMapping(value = "/commentlist/user/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsComment>> list(@PathVariable Long userId) {
        List<PmsComment> articleList = pmsCommentService.getUserCommentlist(userId);
        return CommonResult.success(articleList);
    }
}
